package com.ruoyi.cbs.approval.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.cbs.approval.domain.ApprovalConfig;
import com.ruoyi.cbs.approval.domain.vo.AddApprovalUserReqVO;
import com.ruoyi.cbs.approval.domain.vo.ApprovalReqVO;
import com.ruoyi.cbs.approval.domain.vo.ApprovalUserRespVO;
import com.ruoyi.cbs.approval.domain.vo.RejectedReqVO;
import com.ruoyi.cbs.approval.mapper.ApprovalConfigMapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.project.ApprovalTypeEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cbs.approval.mapper.ApprovalUserMapper;
import com.ruoyi.cbs.approval.domain.ApprovalUser;
import com.ruoyi.cbs.approval.service.IApprovalUserService;

/**
 * 审核人配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-12
 */
@Service
public class ApprovalUserServiceImpl implements IApprovalUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ApprovalUserMapper approvalUserMapper;
    @Autowired
    private ApprovalConfigMapper approvalConfigMapper;

    /**
     * 查询审核人配置
     *
     * @param id 审核人配置主键
     * @return 审核人配置
     */
    @Override
    public ApprovalUser selectApprovalUserById(Long id) {
        return approvalUserMapper.selectApprovalUserById(id);
    }

    /**
     * 查询审核人配置列表
     *
     * @param approvalUser 审核人配置
     * @return 审核人配置
     */
    @Override
    public List<ApprovalUser> selectApprovalUserList(ApprovalUser approvalUser) {
        return approvalUserMapper.selectApprovalUserList(approvalUser);
    }

    /**
     * 查询审批人列表
     * @param approvalType
     * @return
     */
    @Override
    public List<ApprovalUser> selectByApprovalType(String approvalType) {
        return approvalUserMapper.selectByApprovalType(approvalType);
    }

    /**
     * 新增审核人配置
     *
     * @param approvalUser 审核人配置
     * @return 结果
     */
    @Override
    public int insertApprovalUser(ApprovalUser approvalUser) {
        approvalUser.setCreateTime(DateUtils.getNowDate());
        return approvalUserMapper.insertApprovalUser(approvalUser);
    }

    /**
     * 修改审核人配置
     *
     * @param approvalUser 审核人配置
     * @return 结果
     */
    @Override
    public int updateApprovalUser(ApprovalUser approvalUser) {
        approvalUser.setUpdateTime(DateUtils.getNowDate());
        return approvalUserMapper.updateApprovalUser(approvalUser);
    }

    /**
     * 批量删除审核人配置
     *
     * @param ids 需要删除的审核人配置主键
     * @return 结果
     */
    @Override
    public int deleteApprovalUserByIds(Long[] ids) {
        return approvalUserMapper.deleteApprovalUserByIds(ids);
    }

    /**
     * 删除审核人配置信息
     *
     * @param id 审核人配置主键
     * @return 结果
     */
    @Override
    public int deleteApprovalUserById(Long id) {
        return approvalUserMapper.deleteApprovalUserById(id);
    }


    /**
     * 审核节点添加审批人
     * @param reqVO
     * @return
     */
    @Override
    public int addApprovalUsers(AddApprovalUserReqVO reqVO) {
        ApprovalUser approvalUser = new ApprovalUser();
        approvalUser.setConfigId(reqVO.getConfigId());
        approvalUser.setUserId(reqVO.getUserId());
        //获取用户信息详情
        SysUser sysUser = sysUserMapper.selectUserById(reqVO.getUserId());
        //获取审批配置信息详情
        ApprovalConfig approvalConfig = approvalConfigMapper.selectApprovalConfigById(reqVO.getConfigId());
        //获取用户部门信息
        SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());
        approvalUser.setUserName(sysUser.getUserName());
        approvalUser.setDeptId(sysUser.getDeptId());
        approvalUser.setDeptName(sysDept.getDeptName());
        approvalUser.setPhone(sysUser.getPhonenumber());
        approvalUser.setApprovalType(ApprovalTypeEnum.START_APPLU.getCode());
        if (approvalConfig.getCode().length() == 6) {
            approvalUser.setApprovalStep(approvalConfig.getName());
        }
        if (approvalConfig.getCode().length() > 6) {
            ApprovalConfig approvalParentConfig = approvalConfigMapper.selectApprovalConfigById(approvalConfig.getParentId());
            approvalUser.setApprovalStep(approvalParentConfig.getName());
        }
        approvalUser.setDelFlag("0");
        approvalUser.setCreateTime(DateUtils.getNowDate());
        return approvalUserMapper.insertApprovalUser(approvalUser);
    }

    /**
     * 查询审核节点审批人列表
     * @param configId
     * @return
     */
    @Override
    public List<ApprovalUserRespVO> selectApprovalUsersByConfigId(Long configId) {
        List<ApprovalUserRespVO> list = new ArrayList<>();
        List<ApprovalUser> approvalUsers = approvalUserMapper.selectApprovalUsersByConfigId(configId);
        for (ApprovalUser user : approvalUsers) {
            ApprovalUserRespVO approvalUserRespVO = new ApprovalUserRespVO();
            BeanUtils.copyProperties(user, approvalUserRespVO);
            list.add(approvalUserRespVO);
        }
        return list;
    }
}
